/**
 * Copyright (c) 2010
 * Bert Young. UESTC. 
 */

#ifndef BERT_BASESERVER_H
#define BERT_BASESERVER_H

#include <netinet/in.h>
#include <string>
#include "Entry.h"

class BaseServer : public Entry
{
protected:
	/**
	 * Listen foreign connection
	 */
	virtual void listenForeign() = 0;
	/**
	 * Do sth before exit
	 */
	virtual void recycle() = 0;
	/**
	 * Should this server stop?
	 */
	bool terminate;
	/**
	 * Do sth before bind: process signal, create listen socket...
	 */
	virtual bool init();
public:
	/**
	 * Constructor
	 */
	BaseServer(const std::string & name = "BaseServer");
	/**
	 * Destructor
	 */
	virtual ~BaseServer()
	{
		instance = NULL;
	}
	/**
	 * Should this server stop?
	 */
	bool isTerminate() const { return terminate; }
	/**
	 * Stop this server
	 */
	void Terminate()  { terminate = true; }
	/**
	 * Listen and create new TCPTask
	 */
	void mainLoop();
	/**
	 * The main server, listen for foreign connections
	 */
	static BaseServer * instance;
	/**
	 * SIGHUP handler
	 */
	virtual void reloadConfig()
	{
	}
};

#endif
